<?php
/**
 * 文章管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/15
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\ArticleCategoryModel;
use app\common\model\ArticleMessageModel;
use app\common\model\ArticleModel;
use app\common\model\MemberModel;
use app\common\model\SystemMsgModel;
use think\facade\Request;

class ArticleController extends AdminBaseController
{
    /**
     * 文章列表
     */
    public function index(ArticleModel $art){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$art ->getALL($where);

        $plist=ArticleCategoryModel::getALL()['tree'];

        $this->assign("plist",$plist);
        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data){

        $where=[];

        if(!empty($data["title"])){
            $where[]=["title",'like',"%{$data['title']}%"];
        }

        if(!empty($data["cat_id"])){
            $where[]=["cat_id","=",$data["cat_id"]];
        }

        if (!empty($data["status"]) && $data["status"] != -2) {
            $where[] = ["status", '=', $data['status'] - 1];
        }

        if (!empty($data["mobile"])) {
            $u = MemberModel::where('mobile', $data["mobile"])
                ->field('uid')
                ->find();

            $where[] = ["user_id", '=', $u['uid']];
        }

        return $where;
    }

    /**
     * 文章分类
     */
    public function cat(ArticleCategoryModel $cat){
        $list=$cat::getALL()['tree'];

        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param ArticleModel $art
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit($id=0,ArticleModel $art){
        if(Request::isPost()){
            $data=Request::param();

            $status=$art->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=$art::get($id);
            $plist=ArticleCategoryModel::getALL()['tree'];

            $this->assign("info",$info);
            $this->assign("plist",$plist);
            $this->assign("id",$id);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");
        $type=Request::post("type");

        $am=ArticleModel::get($id);

        if(empty($type)){
            $am->status=$status;
        }else{
            $am->hot=$status;
        }

        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function del(ArticleModel $ap){
        $id=Request::post("id");

        $status=$ap->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(ArticleModel $ap){
        $id=Request::post("ids");

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$ap->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function cat_edit($id=0,$pid=0,ArticleCategoryModel $cat){
        if(Request::isPost()){
            $data=Request::param();

            $status=$cat->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=$cat::get($id);
            $plist=$cat::getALL()['tree'];

            $this->assign("info",$info);
            $this->assign("plist",$plist);
            $this->assign("id",$id);
            $this->assign("pid",$pid);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function cat_status(){
        $id=Request::post("id");
        $status=Request::post("status");

        $sta=ArticleCategoryModel::get($id);
        $sta->status=$status;
        $st=$sta->save();

        ArticleCategoryModel::getALL(1);
        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function cat_del(ArticleCategoryModel $cat){
        $id=Request::post("id");

        $status=$cat->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function cat_del_all(ArticleCategoryModel $cat){
        $id=Request::post("ids");

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$cat->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 排序
     */
    public function cat_order(ArticleCategoryModel $cat){
        $id=Request::post("id");
        $orders=Request::post("orders");

        $ad=$cat::get($id);
        $ad->list_order=$orders;
        $ad=$ad->save();

        if($ad){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }

    /**
     * 评论列表
     * @param ArticleMessageModel $am
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function message(ArticleMessageModel $am){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$am ->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 留言详情
     * @param $id
     * @return mixed
     */
    public function msg_detail($id,$t=0){
        $info=ArticleMessageModel::get($id);

        $this->assign('info',$info);
        $this->assign('t',$t);
        return $this->fetch();
    }

    /**
     * 审核评论
     */
    public function msg_status(){
        $id=Request::post("id");
        $status=Request::post("status");
        $content=Request::post("content");

        $sta=ArticleMessageModel::get($id);
        $sta->status=$status;
        $st=$sta->save();

        if($st){
            if($status==-1){
                MemberModel::subGold($id,2);

                if(!empty($content)){
                    $sysData=[
                        'send_user_id'=>$sta["user_id"],
                        'title'=>'[系统消息]您发布的文章留言已经驳回',
                        'user_car_id'=>$sta["article_id"],
                        'content'=>$content,
                        'create_time'=>time(),
                        'status'=>1,
                    ];
                    SystemMsgModel::adds($sysData);
                }
            }

            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }

    /**
     * 批量删除
     */
    public function msg_del_all(ArticleMessageModel $cat){
        $id=Request::post("ids");

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$cat->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }
}